#include "XML_Loader.h"
#include "App.h"
#include "Track.h"
#include "Jet.h"

extern DebugTracer _DEBUGTRACER;
XML_Loader::XML_Loader()
{
    //ctor
}

XML_Loader::~XML_Loader()
{
    //dtor
}

void XML_Loader::LoadFromXmlFile(Track* track,const std::string& filename)
{
    TiXmlDocument doc(filename.c_str());
    doc.LoadFile();

    TiXmlHandle hDoc(&doc);
    TiXmlElement *track_elem = hDoc.FirstChild("Track").Element();
    track->SetName(track_elem->Attribute("name"));

    long counter = 0;
    while(hDoc.FirstChild("Track").Child("node",counter).ToElement() != NULL)
    {
        TiXmlElement* node_elem = hDoc.FirstChild("Track").Child("node",counter).ToElement();
        TrackNode node;
        node_elem->QueryFloatAttribute("size",&node.Size);
        node.Texture = node_elem->Attribute("material");

        TiXmlElement* position_elem = hDoc.FirstChild("Track").Child("node",counter).Child("position",0).ToElement();
        position_elem->QueryFloatAttribute("x",&node.Position.x);
        position_elem->QueryFloatAttribute("y",&node.Position.y);
        position_elem->QueryFloatAttribute("z",&node.Position.z);

        TiXmlElement* normal_elem = hDoc.FirstChild("Track").Child("node",counter).Child("normal",0).ToElement();
        normal_elem->QueryFloatAttribute("x",&node.Normal.x);
        normal_elem->QueryFloatAttribute("y",&node.Normal.y);
        normal_elem->QueryFloatAttribute("z",&node.Normal.z);

        track->AddTrackNode(node.Position,node.Normal,node.Texture,node.Size);
        counter++;
    }
}

void XML_Loader::SaveTrack(Track* track, const std::string& filename)
{
    TiXmlDocument doc;

    TiXmlElement *trackElem = new TiXmlElement("Track");
    trackElem->SetAttribute("name",track->GetName().c_str());
    doc.LinkEndChild(trackElem);

    TiXmlElement *nodeElem = NULL;
    TiXmlElement *posElem = NULL;
    TiXmlElement *normElem = NULL;

    std::vector<TrackNode>::iterator it;
    for(it = track->GetTrackNodeList().begin(); it < track->GetTrackNodeList().end(); it++)
    {
        nodeElem = new TiXmlElement("node");
        nodeElem->SetAttribute("size",(*it).Size);
        nodeElem->SetAttribute("material",(*it).Texture.c_str());

        posElem = new TiXmlElement("position");
        posElem->SetAttribute("x",(*it).Position.x);
        posElem->SetAttribute("y",(*it).Position.y);
        posElem->SetAttribute("z",(*it).Position.z);
        nodeElem->LinkEndChild(posElem);

        normElem = new TiXmlElement("normal");
        normElem->SetAttribute("x",(*it).Normal.x);
        normElem->SetAttribute("y",(*it).Normal.y);
        normElem->SetAttribute("z",(*it).Normal.z);
        nodeElem->LinkEndChild(normElem);

        trackElem->LinkEndChild(nodeElem);

    }


    doc.SaveFile(filename.c_str());

}

void XML_Loader::LoadGameSettings(App* app, const std::string& filename)
{
}

void XML_Loader::SaveGameSettings(App* app, const std::string& filename)
{
}

bool XML_Loader::LoadJetFromXmlFile(Jet* jet, std::string const& filename, std::string const& jet_name)
{
    TiXmlDocument doc(filename.c_str());
    doc.LoadFile();

    TiXmlHandle hDoc(&doc);
    int counter = 0;
    std::string name;
    while(hDoc.ChildElement("Jet",counter).ToElement() != NULL)
    {
        name = hDoc.ChildElement("Jet",counter).ToElement()->Attribute("name");
        if(name == jet_name)
        {
            jet->SetAuthor(hDoc.ChildElement("Jet",counter).ToElement()->Attribute("author"));
            jet->SetMeshName(hDoc.ChildElement("Jet",counter).FirstChild("mesh").ToElement()->Attribute("file"));
            float braking;
            float acceleration;
            float top_speed;
            float handling;
            float shield;
            TiXmlElement* skill = hDoc.ChildElement("Jet",counter).FirstChildElement("skill").ToElement();
            skill->QueryFloatAttribute("acceleration", &acceleration);
            skill->QueryFloatAttribute("top_speed",&top_speed);
            skill->QueryFloatAttribute("handling",&handling);
            skill->QueryFloatAttribute("braking",&braking);
            skill->QueryFloatAttribute("shield",&shield);
            if(jet->AdjustSkills(acceleration,top_speed,handling,shield,braking))
                return true;
            else
                return false;
            break;
        }
        counter++;
    }
    return false;
}
